package cn.dliyy.warehouse.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import cn.dliyy.warehouse.bean.BWarehouse;
import cn.dliyy.warehouse.service.BWarehouseService;

@Controller
public class WarehouseController {
	protected final transient Log log = LogFactory
			.getLog(StudentController.class);
	@Resource
	private BWarehouse warehouse;
	@Resource
	private BWarehouseService warehouseService;

	public WarehouseController() {
	}

	@RequestMapping(value = "/warehouse")
	public String load(ModelMap modelMap) {
		List<Object> list = warehouseService.getAllList();
		modelMap.put("list", list);
		return "/base/warehouse";
	}

	// @ModelAttribute("warehouse")Warehouse warehouse,
	@RequestMapping(value = "/warehouse/add", method = RequestMethod.GET)
	public String editnew(ModelMap modelMap) {
		// Warehouse warehouse = new Warehouse();
		modelMap.addAttribute("warehouse", warehouse);

		return "/base/warehouse_add";
	}

	@RequestMapping(value = "/warehouse/add", method = RequestMethod.POST)
	public String create(@Valid BWarehouse warehouse, BindingResult result,ModelAndView mav) {
		mav.addObject("emsg", "错误信息");
		if (result.hasErrors()) {
			System.out.println("-----------" + result.getAllErrors().get(0).getDefaultMessage());
			return "/base/warehouse_add";
		}
		if (warehouse.getUsed() == null || warehouse.getUsed().equals("")) {
			warehouse.setUsed("0");
		}
		warehouseService.save(warehouse);
		System.out.print("   " + warehouse.getName());
		return "redirect:/warehouse";
	}
	@RequestMapping(value = "/warehouse", params = "m=del")
	public String delete(@RequestParam("id") String id) {
		System.out.println("-----------" + id);
		warehouse.setId(id);
		warehouseService.delete(warehouse);
		return "redirect:/warehouse";
	}

}
